import Taro, {Component} from '@tarojs/taro'
import {
  View,
  Text,
  Button,
  OpenData
} from '@tarojs/components'

import './startPage.scss'
import '../index/index.scss'

import DataManager from '../../utils/DataManager.js'
import {STORAGE_KEY} from "../../utils/const";
import {base_url} from "../../network/config";

export default class startPage extends Component{

  config = {
    navigationBarTitleText: ''
  }

  constructor() {
    super(...arguments)
    this.state = {
      hasAuth: false
    }
  }

  onPullDownRefresh(){
    Taro.stopPullDownRefresh();
  }

  componentWillMount() {
    this.setState({
      hasAuth:DataManager.getHasAuth()
    })
    if (DataManager.getHasAuth()){
      this.login();
    }
  }

  handleAuth = (e) =>{
    console.log('handleAuth-------' + JSON.stringify(e))
    if (e.detail.userInfo){
      Taro.setStorageSync(STORAGE_KEY.HAS_AUTH , true);
      Taro.setStorageSync(STORAGE_KEY.WEIXIN_NICKNAME , e.detail.userInfo.nickName);
      Taro.setStorageSync(STORAGE_KEY.WEIXIN_AVATAR , e.detail.userInfo.avatarUrl);
      this.setState({
        hasAuth:true
      })
      this.login()
    }
  }


  login(){
    Taro.login({
      success(res){
        console.log('weixin login--' + JSON.stringify(res))
        let url = base_url + 'stateless/weixin/weixinAuth'
        Taro.request({
          url:url,
          data: {
            platform:'android_c',
            weixinId:DataManager.getWeiXinId(),
            loginChannel:'wechat',
            appId:'KC876D98B30UA40CJSNHI689FB33SDW',
            digest : 'dda484557c12febe48139d8f44051c13',
            code:res.code
          },
          method: 'GET',
          success(res1) {
            console.log('auth login--' + res1.data.data.openId)
            Taro.request({
              url:base_url + 'login',
              data:{
                platform:'android_c',
                weixinId:DataManager.getWeiXinId(),
                loginChannel:'wechat',
                appId:'KC876D98B30UA40CJSNHI689FB33SDW',
                digest : 'dda484557c12febe48139d8f44051c13',
                username:res1.data.data.openId,
                weixinNickName:DataManager.getWeiXinNickname(),
                weixinHeadImg:DataManager.getWeiXinAvatar()
              },
              header: { 'content-type': 'application/x-www-form-urlencoded' },
              method: 'POST',
              success(res2) {
                console.log('res2=========]' + JSON.stringify(res2))
                if (res2.data.data.token){
                  Taro.setStorageSync(STORAGE_KEY.TOKEN , res2.data.data.token);
                }
                Taro.setStorageSync(STORAGE_KEY.IS_BIND_MOBILE , res2.data.data.isBindMobile)
                Taro.setStorageSync(STORAGE_KEY.IS_BIND_WECHAT , res2.data.data.isBindWechat)
                Taro.setStorageSync(STORAGE_KEY.MOBILE , res2.data.data.mobile)
                Taro.setStorageSync(STORAGE_KEY.IS_BUSINESS , res2.data.data.isBusiness)
                Taro.setStorageSync(STORAGE_KEY.ADDRESS_NAME , res2.data.data.addressName)
                Taro.setStorageSync(STORAGE_KEY.BUSINESS_MOBILE , res2.data.data.businessMobile)
                Taro.setStorageSync(STORAGE_KEY.PROXY_MOBILE , res2.data.data.proxyMobile)
                Taro.setStorageSync(STORAGE_KEY.PROXY_WEIXIN_CODE , res2.data.data.weixinCode)

                Taro.switchTab({url: '/pages/index/index'})
              }
            })
          }
        })
      }
    })
  }

  render(){
    return(
      <View className='start-container'>
        {!this.state.hasAuth &&
        <View className='home-auth-container'>
          <OpenData className='home-auth-image' type='userAvatarUrl'/>
          <OpenData className='home-auth-name' type='userNickName'/>
          <Text style='margin-top:30px;margin-left:20px;margin-right:20px'>
            为了您的正常使用，部分功能需要您授权访问用户信息才可使用
          </Text>
          <Button open-type='getUserInfo' onGetUserInfo={this.handleAuth.bind(this)}>我知道了</Button>
        </View>}
      </View>
    )
  }
}

